import { ajaxService } from "../index";
import type * as Type from "./type";

export async function refreshToken() {
  const res = await ajaxService({
    url: "/user/refresh",
    method: "get",
    params: {
      refresh_token: localStorage.getItem("refresh_token"),
    },
  });

  localStorage.setItem("access_token", res.data.access_token);
  localStorage.setItem("refresh_token", res.data.refresh_token);
  return res;
}

export async function login(username: string, password: string) {
  return ajaxService({
    url: "/user/login",
    method: "POST",
    data: {
      username,
      password,
    },
  });
}

export async function registerCaptcha(params: Type.TobtainCaptcha) {
  return await ajaxService({
    url: "/user/obtain-captcha",
    method: "get",
    params,
  });
}

export async function register(registerUser: Type.TRegisterUser) {
  return await ajaxService({
    url: "/user/register",
    method: "POST",
    data: registerUser,
  });
}

export async function prettier(data: Type.TUpdatePassword) {
  return await ajaxService({
    url: "/user/update_password",
    method: "put",
    data,
  });
}

export async function getUserInfo() {
  return await ajaxService({
    url: "/user/info",
    method: "get",
  });
}

export async function updateInfo(data: Type.UserInfo) {
  return await ajaxService({
    url: "/user/update",
    method: "post",
    data,
  });
}

export async function updatePassword(data: Type.TUpdatePassword) {
  return await ajaxService({
    url: "/user/update_password",
    method: "post",
    data,
  });
}
